<template>
  <div class="app-container">
    <!-- 高级查询 -->
    <div class="filter_container">
         <el-tabs v-model="queryParams.status" type="card" @tab-click="handleQuery">
          <el-tab-pane name="all">
             <span slot="label"><i class="el-icon-bank-card" /> 全部</span>
           </el-tab-pane>
           <el-tab-pane name="applied">
             <span slot="label"><i class="el-icon-bank-card" /> 申请中</span>
           </el-tab-pane>

           <el-tab-pane name="proved">
             <span slot="label"><i class="el-icon-bank-card" /> 已审核</span>
           </el-tab-pane>
         <el-tab-pane name="settled">
           <span slot="label"><i class="el-icon-bank-card" /> 已结算</span>
         </el-tab-pane>


         </el-tabs>
        </div>
    <el-form v-show="searchVisible" ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item label="融资单单号:">
        <el-input v-model="queryParams.financeOrderNo" size="small" placeholder="请输入融资单单号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="资金供应公司:">
        <el-input v-model="queryParams.financeProvideCompanyName" size="small" placeholder="请输入资金供应公司" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="惠单号:">
        <el-input v-model="queryParams.creditBillNo" size="small" placeholder="请输入惠单号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="创建时间:">
        <el-date-picker v-model="queryParams.createDate" type="date" value-format="yyyy-MM-dd" size="small" placeholder="选择创建时间" clearable />
      </el-form-item>

      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
      <el-button v-if="queryParams.status === `proved`" type="success" icon="el-icon-search" size="mini" :disabled="isMultiple" @click="handlePlatformSettle">结算下账</el-button>
     </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermission="['baoli:finFinanceApply:update']" type="success" icon="el-icon-edit" size="mini" :disabled="isSingle" @click="handleEdit">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermission="['baoli:finFinanceApply:delete']" type="danger" icon="el-icon-delete" size="mini" :disabled="isMultiple" @click="handleRemove">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermission="['baoli:finFinanceApply:export']" type="warning" icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
      </el-col>
      <right-toolbar :show-search.sync="searchVisible" @queryTable="handleQuery" />
    </el-row>

    <!-- 数据列表 -->
    <el-table v-loading="loading" :data="page.records" highlight-current-row @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="融资单单号" align="center" prop="financeOrderNo" />
      <el-table-column label="融资天数" align="center" prop="financeDays" />
      <el-table-column label="融资金额" align="center" prop="financeAmt" />
      <el-table-column label="申请公司" align="center" prop="financeApplyCompany" />
      <el-table-column label="资金供应公司" align="center" prop="financeProvideCompanyName" />
      <el-table-column label="利息金额" align="center" prop="interestAmt" />
      <el-table-column label="利率" align="center" prop="interestRate" />
     <el-table-column label="状态" align="center" width="90">
          <template slot-scope="scope">
             <span>{{ scope.row.status | finFinanceApplyFilter }}</span>
           </template>
         </el-table-column>
      <el-table-column label="惠单号" align="center" prop="creditBillNo" />

      <el-table-column label="创建时间" align="center" prop="createDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="text" v-hasPermission="['baoli:finFinanceApply:settlementConfirm']" v-if="handleProveBtnShow(scope.row.status)" icon="el-icon-edit" @click="handlePlatformSettle(scope.row)">结算下账</el-button>

       <!--   <el-dropdown type="text" split-button>
          <el-button type="text" v-hasPermission="['baoli:finFinanceApply:settlementConfirm']" v-if="handleProveBtnShow(scope.row.status)" icon="el-icon-edit" @click="handlePlatformSettle(scope.row)">结算下账</el-button>
           </el-dropdown-menu>   </el-dropdown> -->
          </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination v-show="page.total>0" :total="page.total" :page.sync="page.current" :limit.sync="page.pageSize" @pagination="handleQuery" />

    <!-- 添加/修改对话框 -->
    <el-dialog :title="finFinanceApplyFormDialog.title" :visible.sync="finFinanceApplyFormDialog.visible" :close-on-click-modal="false" :before-close="resetFinFinanceApplyFormDialog" width="500px" append-to-body>
      <el-form ref="finFinanceApplyForm" :model="finFinanceApplyFormData" :rules="finFinanceApplyFormRules" label-width="80px">
        <el-form-item label="融资单单号" prop="financeOrderNo">
          <el-input v-model="finFinanceApplyFormData.financeOrderNo" placeholder="请输入融资单单号" />
        </el-form-item>
        <el-form-item label="融资天数" prop="financeDays">
          <el-input v-model="finFinanceApplyFormData.financeDays" placeholder="请输入融资天数" />
        </el-form-item>
        <el-form-item label="" prop="financeAmt">
          <el-input v-model="finFinanceApplyFormData.financeAmt" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="融资申请公司编号" prop="financeApplyCompanyNo">
          <el-input v-model="finFinanceApplyFormData.financeApplyCompanyNo" placeholder="请输入融资申请公司编号" />
        </el-form-item>
        <el-form-item label="" prop="financeApplyCompany">
          <el-input v-model="finFinanceApplyFormData.financeApplyCompany" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="资金供应公司" prop="financeProvideCompanyName">
          <el-input v-model="finFinanceApplyFormData.financeProvideCompanyName" placeholder="请输入资金供应公司" />
        </el-form-item>
        <el-form-item label="融资资金供应公司编号" prop="financeProvideCompanyNo">
          <el-input v-model="finFinanceApplyFormData.financeProvideCompanyNo" placeholder="请输入融资资金供应公司编号" />
        </el-form-item>
        <el-form-item label="融资金额" prop="interestAmt">
          <el-input v-model="finFinanceApplyFormData.interestAmt" placeholder="请输入融资金额" />
        </el-form-item>
        <el-form-item label="融资利息" prop="interestRate">
          <el-input v-model="finFinanceApplyFormData.interestRate" placeholder="请输入融资利息" />
        </el-form-item>
        <el-form-item label="惠单号" prop="creditBillNo">
          <el-input v-model="finFinanceApplyFormData.creditBillNo" placeholder="请输入惠单号" />
        </el-form-item>
        <el-form-item label="" prop="remark">
          <el-input v-model="finFinanceApplyFormData.remark" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="创建时间" prop="createDate">
          <el-date-picker v-model="finFinanceApplyFormData.createDate" type="date" value-format="yyyy-MM-dd" size="small" style="width: 100%" placeholder="选择创建时间" clearable />
        </el-form-item>
        <el-form-item label="更新时间" prop="updateDate">
          <el-date-picker v-model="finFinanceApplyFormData.updateDate" type="date" value-format="yyyy-MM-dd" size="small" style="width: 100%" placeholder="选择更新时间" clearable />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirmFinFinanceApplyFormDialog">确 定</el-button>
        <el-button @click="resetFinFinanceApplyFormDialog">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import FinFinanceApplyApi from '@/api/baoli/FinFinanceApplyApi'
import appConstant from '@/constant/AppConstant'
const defaultFinFinanceApplyFormData = {
  financeOrderNo: null,

  financeDays: null,

  financeAmt: null,

  financeApplyCompanyNo: null,

  financeApplyCompany: null,

  financeProvideCompanyName: null,

  financeProvideCompanyNo: null,

  interestAmt: null,

  interestRate: null,

  status: '0',

  creditBillNo: null,

  createDate: null,

  updateDate: null,

}
export default {
  name: 'FinFinanceApplyList',
  filters: {
    finFinanceApplyFilter(item) {
      const values = appConstant.finFinanceApplyList.filter(value => value.key === item)
      if (values && values.length > 0) {
        return values[0].label
      } else {
        return ''
      }
    }
   },
  data() {
    return {
      options: {
        sysYnTypeOptions: this.$constant.sysYnTypeOptions
      },
      queryParams: {
        financeOrderNo: null,
        financeDays: null,
        financeAmt: null,
        financeApplyCompanyNo: null,
        financeApplyCompany: null,
        financeProvideCompanyName: null,
        financeProvideCompanyNo: null,
        interestAmt: null,
        interestRate: null,
        status: null,
        creditBillNo: null,
        createDate: null,
        updateDate: null,
        isValid: this.$constant.Y
      },
      page: {
        current: 1,
        pageSize: 10,
        total: 0,
        records: []
      },
      finFinanceApplyFormDialog: {
        title: '',
        visible: false
      },
      finFinanceApplyFormData: Object.assign({}, defaultFinFinanceApplyFormData),
      finFinanceApplyFormRules: {
        createDate: [{ required: true, message: '创建时间不能为空', trigger: 'blur' }],

        updateDate: [{ required: true, message: '更新时间不能为空', trigger: 'blur' }],

      },
      loading: true,
      searchVisible: true,
      multipleSelection: [],
      isSingle: true,
      isMultiple: true
    }
  },
  created() {
    this.handleQuery()
    this.loadOptions()
  },
  methods: {
    loadOptions() {
    },
    handleAdd() {
      this.openFinFinanceApplyFormDialog()
    },
    handlePlatformSettle(row) {
       const ids = row.id || this.multipleSelection.map(item => item.id)
       const params = {
          ids: ids
       }
       FinFinanceApplyApi.settlementConfirm(params).then(() => {
          this.$message.success('结算成功!')
      })
        this.handleQuery()
    },

    handleRemove(row) {
      const ids = row.id || this.multipleSelection.map(item => item.id)
      this.$confirm('是否确认删除数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return FinFinanceApplyApi.remove(ids)
      }).then(() => {
        this.handleQuery()
        this.$message.success('删除成功')
      })
    },
    handleEdit(row) {
      const id = row.id || this.multipleSelection.map(item => item.id)[0]
      this.openFinFinanceApplyFormDialog(id)
    },
    handleProveBtnShow: function(status) {
      const validStatusList = ['proved']
      const index = validStatusList.indexOf(status)
      if (index === -1) {
        return false
      } else {
        return true
      }
    },
    handleQuery() {
      this.loading = true
      FinFinanceApplyApi.page(this.queryParams, this.page).then(response => {
        this.page.records = response.data.records
        this.page.total = response.data.total
        this.loading = false
      }, () => {
        this.loading = false
      })
    },
    handleExport() {
      this.$confirm('是否确认导出数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return FinFinanceApplyApi.exportData(this.queryParams)
      }).then(response => {
        this.$common.download(response.msg)
      })
    },
    openFinFinanceApplyFormDialog(id) {
      this.finFinanceApplyFormDialog.visible = true
      if (id) {
        this.finFinanceApplyFormDialog.title = '融资申请单-修改'
        FinFinanceApplyApi.get(id).then(response => {
          this.finFinanceApplyFormData = {
            ...this.finFinanceApplyFormData,
            ...response.data
          }
        })
      } else {
        this.finFinanceApplyFormDialog.title = '融资申请单-添加'
      }
    },
    confirmFinFinanceApplyFormDialog() {
      this.$refs['finFinanceApplyForm'].validate(valid => {
        if (valid) {
          if (this.finFinanceApplyFormData.id) {
            FinFinanceApplyApi.edit(this.finFinanceApplyFormData).then(response => {
              this.$message.success('修改成功')
              this.resetFinFinanceApplyFormDialog()
              this.handleQuery()
            })
          } else {
            FinFinanceApplyApi.add(this.finFinanceApplyFormData).then(response => {
              this.$message.success('添加成功')
              this.resetFinFinanceApplyFormDialog()
              this.handleQuery()
            })
          }
        }
      })
    },
    resetFinFinanceApplyFormDialog() {
      this.finFinanceApplyFormDialog = {
        title: '',
        visible: false
      }
      this.finFinanceApplyFormData = Object.assign({}, defaultFinFinanceApplyFormData)
      this.resetForm('finFinanceApplyForm')
    },
    handleSelectionChange(selection) {
      this.multipleSelection = selection
      this.isSingle = selection.length !== 1
      this.isMultiple = !selection.length
    },
    resetQuery() {
      this.page.current = 1
      this.resetForm('queryForm')
      this.handleQuery()
    }
  }
}
</script>
